﻿using Q42.WinRT.Data;
using SpaceNeedle.SDK.Windows.Platform.Commands;
using SpaceNeedle.SDK.Windows.Platform.Data;
using SpaceNeedle.x6473.WindowsPhone.Common;
using SpaceNeedle.x6473.WindowsPhone.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace SpaceNeedle.x6473.WindowsPhone.ViewModels
{
    public class TicketsViewModel : GlobalViewModel
    {
        public TicketsViewModel() :base()
        {
            
        }

        public AppContent.TicketsInfo StaticContent
        {
            get { return this.Get<AppContent.TicketsInfo>(); }
            set { this.Set(value); }
        }

        public string PageTitle
        {
            get { return this.Get<string>(); }
            set { this.Set(value); }
        }

        public ObservableCollection<RequestItem> RequestItems
        {
            get { return this.Get<ObservableCollection<RequestItem>>(); }
            set { this.Set(value); }
        }
       
        protected async override void Load()
        {
            this.IsInProgress = true;

            await base.LoadAsync();

            this.StaticContent = this.AppContent.Tickets;

            this.RequestItems = await AppViewModel.Instance.LoadUserRequests();

            Tracking.SendView(ViewsNames.Tickets);

            this.IsInProgress = false;
        }

        private async Task<ObservableCollection<RequestItem>> LoadRequestItems()
        {
            var result4 = await new WebRequest().DownloadApiStringAsync<ObservableCollection<RequestItem>>("http://6473.azurewebsites.net/api/requestitems?user=" + System.Net.HttpUtility.UrlEncode(AppViewModel.Instance.Properties.Me.Id.ToString()), "GET", null, null, null);

            if (result4.Status == 200)
            {
                return result4.Content;
            }

            return null;
        }

        private static async Task<AppContent> LoadAppContent()
        {
            var result0 = await new WebRequest().DownloadApiStringAsync<AppContent>("http://6473.azurewebsites.net/api/appcontents/pt-br", "GET", null, null, null);

            if (result0.Status == 200)
            {
                return result0.Content;
            }

            return null;
        }
    }
}
